﻿//-----------------------------------------------------------------------
// <copyright file="SplashScreenForm.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace App.UI
{
    using System.ComponentModel;
    using System.Windows.Forms;
    using TileTool.Log;

    /// <summary>
    /// Application splash screen form.
    /// </summary>
    public partial class SplashScreenForm : Form
    {
        #region Fields - Private

        /// <summary>
        /// Async operation for posting to the UI.
        /// </summary>
        private AsyncOperation op;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SplashScreenForm()
        {
            this.InitializeComponent();

            this.CenterToScreen();

            this.op = AsyncOperationManager.CreateOperation(null);

            this.FormClosed += this.HandleFormClosed;

            LogSystem.Manager.NewLog += this.HandleStartupLog;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Set the status text of the splash screen.
        /// </summary>
        /// <param name="status">Spash status text.</param>
        public void SetStatus(string status)
        {
            this.op.Post(
                (object s) =>
                {
                    statusLabel.Text = status;
                    statusLabel.Invalidate();
                },
                null);
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Handle a startup log.
        /// </summary>
        /// <param name="data">Log data.</param>
        private void HandleStartupLog(LogData data)
        {
            if (data.Level != LogLevel.Startup)
            {
                return;
            }

            this.SetStatus(data.Message);
        }

        /// <summary>
        /// Handle the form being closed.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        private void HandleFormClosed(object sender, FormClosedEventArgs e)
        {
            LogSystem.Manager.NewLog -= this.HandleStartupLog;
            this.op.OperationCompleted();
        }

        #endregion
    }
}
